<?php

namespace backend\models\user;

use Yii;

/**
 * This is the model class for table "{{%edu_teacher_info}}".
 *
 * @property integer $teacher_id
 * @property string $teacher_name
 * @property string $teacher_thumb
 * @property string $teacher_desc
 * @property integer $position_id
 * @property integer $from_id
 * @property string $teacher_at
 * @property integer $recommend
 * @property integer $recommend_time
 */
class TeacherInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%teacher_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position_id', 'from_id', 'recommend', 'recommend_time'], 'integer'],
            [['teacher_name'], 'string', 'max' => 20],
            [['teacher_thumb'], 'string', 'max' => 50],
			[['teacher_thumb'], 'string', 'max' => 50],
            [['teacher_desc', 'teacher_at'], 'string', 'max' => 255],
			[['teacher_thumb'], 'file', 'extensions' => 'gif, jpg,png,pneg','maxSize'=>1024 * 1024 * 1,'message'=>'{attribute}上传图片必须是gif, jpg,png,pneg，不能超过1M' ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'teacher_id' => 'Teacher ID',
            'teacher_name' => '教师名称',
            'teacher_thumb' => '教师头像',
            'teacher_desc' => '教师描述',
            'position_id' => '教师职位',
            'from_id' => '教师来源',
            'teacher_at' => '擅长领域',
            'recommend' => 'Recommend',
            'recommend_time' => 'Recommend Time',
        ];
    }
	 /**
     * 修改是否推荐老师
     */
    public function TeacherInfoRecommend($id,$status)
	{
		if($status == 1){
			return $this->updateAll(['recommend' => $status,'recommend_time' => time()],'teacher_id=:teacher',[':teacher'=>$id]);
		}else{
			return $this->updateAll(['recommend' => $status,'recommend_time' => ''],'teacher_id=:teacher',[':teacher'=>$id]);
		}
		
	}
	/**
	* 查询
	*/
	public function TeacherCount($code)
	{
		return $this->find()
					->where($code)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function TeacherSearch($code,$pagination)
    {
		return $this->find()
				->where($code)
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}
	/**
	* 查询管理员详情
	*/
	public function TeacherInfo($id)
	{
		return $this->find()
				->where(['=','teacher_id',$id])
				->asArray()
				->one();
	}

	/* 查询推荐老师 */
	static public function Teacherrmd($limit)
	{
		return 	TeacherInfo::find()
				->select("*")
				->innerjoin("`edu_teacher_position` on `edu_teacher_info`.`position_id` = `edu_teacher_position`.`position_id`")
				->innerjoin('`edu_admin` on `edu_teacher_info`.`teacher_id` = `edu_admin`.`id`')
				->where("recommend = 1 and status = 10")
				->limit($limit)
				->orderBy(['recommend_time'=>SORT_DESC])
				->asArray()
				->all();
	}
	//总条数
	public function Count($cond)
    {
		return $this->find()
					->select('*')
					->innerjoin('`edu_admin` on `edu_teacher_info`.`teacher_id` = `edu_admin`.`id`')
					->where($cond)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function Search($cond,$pagination)
    {
		return $this->find()
				->select('*')
				->innerjoin('`edu_admin` on `edu_teacher_info`.`teacher_id` = `edu_admin`.`id`')
				->orderBy(['recommend'=>SORT_DESC])
				->where($cond)
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}
}
